define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'agentsystem/withdrawals/index',
                    add_url: 'agentsystem/withdrawals/add',
                    edit_url: 'agentsystem/withdrawals/edit',
                    del_url: 'agentsystem/withdrawals/del',
                    multi_url: 'agentsystem/withdrawals/multi',
                    table: 'y_withdrawals',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'ID',
                sortName: 'ID',
                search: false,
                columns: [
                    [
                        {field: 'orderNo', title: __('Orderno'), operate: false},
                        {field: 'uid', title: __('Uid')},
                        {field: 'nickname', title: __('Nickname'), operate: false},
                        {field: 'money', title: __('Money'), operate: false},
                        {field: 'status', title: __('Status'), searchList: {'0':'待确认','1':'已确认', '2': '转帐'}},
                        {field: 'addtime', title: __('Addtime'), formatter: Table.api.formatter.datetimes, operate: 'BETWEEN', type: 'datetime', addclass: 'datetimepicker', data: 'data-date-format="YYYY-MM-DD"', style: 'min-width:90px;'},
                        {field: 'confirmtime', title: __('Confirmtime'), formatter: Table.api.formatter.datetimes, operate: false},
                        {field: 'successtime', title: __('Successtime'), formatter: Table.api.formatter.datetimes, operate: false},
                        {field: 'operate', title: __('Operate'), table: table,events: Table.api.events.operate,
                        	formatter: function (value, row, index) {
                        		var res = '';
                        		res = '<a title="详情" class="btn btn-xs btn-primary btn-dialog" href="/admin/agentsystem/withdrawals/detail/ids/'+row.ID+'"> 详情</a>';
            				if(row.status == '待确认'){
            					res = res + ' <a title="确认" class="btn btn-xs btn-success btn-status" href="/admin/agentsystem/withdrawals/setStatusValue/ids/'+row.ID+'/status/1"> 确认</a>';
            				}
            				if(row.status == '已确认'){
            					res = res + ' <a title="转账" class="btn btn-xs btn-info btn-status" href="/admin/agentsystem/withdrawals/setStatusValue/ids/'+row.ID+'/status/2"> 转账</a>';
            				}
            				return res;
						}}
                    ]
                ],
            
	            onLoadSuccess: function (data) {
					if(data.map == 1){
						$("#statistics").html(
								'<a class="btn btn-danger">总提现：' + data.statistics.totalall + '</a>&nbsp;' 
								);
		            	$("#statistics").show();
		            }else{
						$("#statistics").html(
								'<a class="btn btn-danger">总提现：' + data.statistics.totalall + '</a>&nbsp;' 
								+ '<a class="btn btn-success">月提现：' + data.statistics.totalmonth + '</a>&nbsp;' 
								+ '<a class="btn btn-info">周提现：' + data.statistics.totalweek + '</a>&nbsp;' 
								+ '<a class="btn btn-warning">今日提现：' + data.statistics.totaltoday + '</a>&nbsp;' 
								);
		            	$("#statistics").show();
		            }
	            }
            
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            
            //点击包含.btn-ajax的元素时发送Ajax请求
            $(document).on('click', '.btn-status', function (e) {
                var options = $(this).data();
                if (typeof options.url === 'undefined' && $(this).attr("href")) {
                    options.url = $(this).attr("href");
                }
                $.post(options.url).success(function(data){
                    if (data.code==1) {
                        if (data.msg) {
                        	Layer.alert(data.msg);
                        }
                    	table.bootstrapTable('refresh');
                    }else{
                    	Layer.alert(data.msg);
                    }
                });
                return false;
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});